package system

import (
	"encoding/json"
	"sensitives/service/dic"
	"strconv"

	"github.com/gin-gonic/gin"
)

type vms struct {
	commonfilter
}

var Vms vms

func (filter *vms) Incision(ginc *gin.Context) {
	siteid, _ := strconv.Atoi(ginc.Param("siteid"))
	type Params struct {
		Alias       string `form:"alias"`
		Description string `form:"description"`
		Keyword     string `form:"keyword"`
	}
	var params Params
	err := ginc.Bind(&params)
	filter.Sensitiveword = make([]sensitive, 0)
	type OutPut struct {
		Code bool        `json:"code"`
		Data []sensitive `json:"data"`
	}
	output := OutPut{Code: false, Data: filter.Sensitiveword}
	stive_sys, _ := json.Marshal(output)
	if err != nil {
		ginc.Writer.Write(stive_sys)
		return
	}
	site_sys_dic := dic.CloudMap[siteid].Sys
	if params.Alias != "" {
		filter.filter(params.Alias, "alias", &site_sys_dic)
	}
	if params.Description != "" {
		filter.filter(params.Description, "description", &site_sys_dic)
	}
	if params.Keyword != "" {
		filter.filter(params.Keyword, "keyword", &site_sys_dic)
	}
	output = OutPut{Code: true, Data: filter.Sensitiveword}
	stive_sys, _ = json.Marshal(output)
	ginc.Writer.Write(stive_sys)
}

func (filter *vms) Record(ginc *gin.Context) {
	siteid, _ := strconv.Atoi(ginc.Param("siteid"))
	name := ginc.PostForm("name")
	filter.Sensitiveword = make([]sensitive, 0)
	type OutPut struct {
		Code bool        `json:"code"`
		Data []sensitive `json:"data"`
	}
	output := OutPut{Code: false, Data: filter.Sensitiveword}
	stive_sys, _ := json.Marshal(output)
	if name == "" {
		ginc.Writer.Write(stive_sys)
		return
	}
	site_sys_dic := dic.CloudMap[siteid].Sys
	filter.filter(name, "name", &site_sys_dic)
	output = OutPut{Code: true, Data: filter.Sensitiveword}
	stive_sys, _ = json.Marshal(output)
	ginc.Writer.Write(stive_sys)
}
